var app = getApp()
import {
  list
} from '../../api/shop.js';
const util = require('../../utils/util')
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    shopList: [],
    from: {}
  },

  /**
   * 组件的方法列表
   */
  attached() {
    this.loadList()
  },
  methods: {
    loadList() {
      var _this = this
      let params = {
        mchId: app.globalData.mchId,
      }
      if (!util.isNull(app.globalData.lat) && !util.isNull(app.globalData.lng)) {
        params.lat = app.globalData.lat
        params.lng = app.globalData.lng
      }
      list(params).then(res => {
        _this.data.shopList = res.data
        _this.data.from = res.data[0]
        _this.data.shopList.map(el => {
          el.distance = _this.distanceFormat(el.distance)
        })
        _this.setData({
          from: _this.data.from,
          shopList: _this.data.shopList
        })


      })
    },
    chooseClick(data) {
      var _this = this
      _this.data.shopIndex = data.currentTarget.dataset.index
      _this.data.from = data.currentTarget.dataset.item
      _this.setData({
        from: _this.data.from,
        shopIndex: _this.data.shopIndex
      })
    },
    completeClick() {
      var from = {
        from: this.data.from,
        shopIndex: this.data.shopIndex
      }
      console.log(from)
      this.triggerEvent("completeSite", from)
    },
    cencleClick() {
      this.triggerEvent("cencleSite")
    },
    distanceFormat(distance) {
      if (!distance && distance == null) {
        return '';
      } else if (distance < 1) {
        return (distance * 1000).toFixed(0) + 'm';
      } else {
        return distance.toFixed(1) + 'km';
      }
    },
  }
})
